﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class DomainDA : IDomainDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCRIPTION = "@DESCRIPTION";
        private string DOMAIN_ID = "@DOMAIN_ID";
        private string LEVEL_NUM = "@LEVEL_NUM";
        private string NAME = "@NAME";
        private string PARENT_DOMAIN_ID = "@PARENT_DOMAIN_ID";
        private string SQL_CHECK_DOMAIN_ID_UNIQUE = " SELECT COUNT(1) FROM @_@DOMAIN WHERE DOMAIN_ID = @DOMAIN_ID ";
        private string SQL_DELETE_DOMAIN = " DELETE FROM @_@DOMAIN WHERE DOMAIN_ID = @DOMAIN_ID ";
        private string SQL_INSERT_DOMAIN = " INSERT INTO @_@DOMAIN ( WH_ID, DOMAIN_ID, NAME, DESCRIPTION, PARENT_DOMAIN_ID, LEVEL_NUM, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @DOMAIN_ID, @NAME, @DESCRIPTION, @PARENT_DOMAIN_ID, @LEVEL_NUM, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_DOMAIN = " SELECT  ROWID, WH_ID, DOMAIN_ID, NAME, DESCRIPTION, PARENT_DOMAIN_ID, LEVEL_NUM, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@DOMAIN ";
        private string SQL_UPDATE_DOMAIN = " UPDATE @_@DOMAIN SET WH_ID = @WH_ID, NAME = @NAME, DESCRIPTION = @DESCRIPTION, PARENT_DOMAIN_ID = @PARENT_DOMAIN_ID, LEVEL_NUM = @LEVEL_NUM, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE DOMAIN_ID = @DOMAIN_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckDomainIDUnique(string domainID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOMAIN_ID", domainID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_DOMAIN_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckDomainIDUnique(DataBase dataBase, DbTransaction tran, string domainID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOMAIN_ID", domainID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_DOMAIN_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteDomain(List<string> domainID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < domainID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOMAIN_ID", domainID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DOMAIN.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteDomain(DataBase dataBase, DbTransaction tran, List<string> domainID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < domainID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOMAIN_ID", domainID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DOMAIN.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllDomain(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DOMAIN.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllDomain(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DOMAIN.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public DomainInfo GetDomainByID(string domainID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DOMAIN.Replace("@_@", whLoginID) + " WHERE DOMAIN_ID = @DOMAIN_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOMAIN_ID", domainID) };
            DomainInfo domainInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    domainInfo = new DomainInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), (reader["DOMAIN_ID"].ToString() != "") ? int.Parse(reader["DOMAIN_ID"].ToString()) : 0, reader["NAME"].ToString(), reader["DESCRIPTION"].ToString(), (reader["PARENT_DOMAIN_ID"].ToString() != "") ? int.Parse(reader["PARENT_DOMAIN_ID"].ToString()) : 0, (reader["LEVEL_NUM"].ToString() != "") ? int.Parse(reader["LEVEL_NUM"].ToString()) : 0, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return domainInfo;
        }

        public DomainInfo GetDomainByID(DataBase dataBase, DbTransaction tran, string domainID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DOMAIN.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOMAIN_ID", domainID) };
            DomainInfo domainInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                domainInfo = new DomainInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), (reader["DOMAIN_ID"].ToString() != "") ? int.Parse(reader["DOMAIN_ID"].ToString()) : 0, reader["NAME"].ToString(), reader["DESCRIPTION"].ToString(), (reader["PARENT_DOMAIN_ID"].ToString() != "") ? int.Parse(reader["PARENT_DOMAIN_ID"].ToString()) : 0, (reader["LEVEL_NUM"].ToString() != "") ? int.Parse(reader["LEVEL_NUM"].ToString()) : 0, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return domainInfo;
        }

        public DataSet GetDomainByQueryList(List<string> sqlWhere, DomainQueryEntity domainQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_DOMAIN.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!domainQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, domainQuery.CurrentPage, domainQuery.PageSize, domainQuery.SortField, domainQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetDomainByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, DomainQueryEntity domainQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_DOMAIN.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!domainQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, domainQuery.CurrentPage, domainQuery.PageSize, domainQuery.SortField, domainQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertDomain(DomainInfo domainInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    domainInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Domain_Parameters(domainInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_DOMAIN.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertDomain(DataBase dataBase, DbTransaction tran, DomainInfo domainInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    domainInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Domain_Parameters(domainInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_DOMAIN.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Domain_Parameters(DomainInfo domainInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.DOMAIN_ID, SqlDbType.Int, 4), new SqlParameter(this.NAME, SqlDbType.VarChar, 20), new SqlParameter(this.DESCRIPTION, SqlDbType.VarChar, 50), new SqlParameter(this.PARENT_DOMAIN_ID, SqlDbType.Int, 4), new SqlParameter(this.LEVEL_NUM, SqlDbType.Int, 4), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(domainInfo.WhID))
            {
                paramArray[0].Value = domainInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = domainInfo.DomainID;
            paramArray[2].Value = domainInfo.Name;
            paramArray[3].Value = domainInfo.Description;
            paramArray[4].Value = domainInfo.ParentDomainID;
            paramArray[5].Value = domainInfo.LevelNum;
            paramArray[6].Value = domainInfo.CreatedBy;
            paramArray[7].Value = domainInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateDomain(DomainInfo domainInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    domainInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Domain_Parameters(domainInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_DOMAIN.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateDomain(DataBase dataBase, DbTransaction tran, DomainInfo domainInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    domainInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Domain_Parameters(domainInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_DOMAIN.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

